/*
 * @Description: 
 * @Author: jack-wang
 * @Date: 2021-03-29 13:11:11
 * @LastEditTime: 2021-03-29 13:39:48
 * @LastEditors: Su Yunzheng
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    meta:{
      title:'登录',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/index.vue')
  },
  {
    path: '/registry',
    name: 'registry',
    meta:{
      title:'注册',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "registry" */ '@/views/registry/index.vue')
  },
  {
    path: '/main',
    name: 'main',
    meta:{
      title:'主页面',
      keepAlive:true
    },
    component: () => import(/* webpackChunkName: "main" */ '@/views/main/index.vue'),
    children:[
      {
        path:'/main/home',
        name:'home',
        meta:{
          title:'首页',
          keepAlive:true,
        },
        component: () => import(/* webpackChunkName: "home" */ '@/views/main/home/index.vue'),
      },
      {
        path:'/main/classify',
        name:'classify',
        meta:{
          title:'分类',
          keepAlive:true,
        },
        component: () => import(/* webpackChunkName: "classify" */ '@/views/main/classify/index.vue'),
      },
      {
        path:'/main/car',
        name:'car',
        meta:{
          title:'购物车',
          keepAlive:true,
        },
        component: () => import(/* webpackChunkName: "home" */ '@/views/main/car/index.vue'),
      },
      {
        path:'/main/my',
        name:'my',
        meta:{
          title:'我的',
          keepAlive:true,
        },
        component: () => import(/* webpackChunkName: "my" */ '@/views/main/my/index.vue'),
      },
      {
        path:'/main',
        redirect:'/main/home'
      }
    ],
    beforeEnter: (to, from, next) => {
        let positionData = localStorage.getItem('positionData') || '';
        if(positionData){
            //定位成功
            next()
        }else{
            next('/');
        }
    }
  },
  {
    path: '/detail',
    name: 'detail',
    meta:{
      title:'详情',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "detail" */ '@/views/detail/index.vue'),
  },
  {
    path: '/search',
    name: 'search',
    meta:{
      title:'搜索',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "search" */ '@/views/search/index.vue'),
  },
  {
    path: '/addAddress',
    name: 'addAddress',
    meta:{
      title:'添加地址',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "addAddress" */ '@/views/addAddress/index.vue'),
  },
  {
    path: '/',
    name: 'position',
    meta:{
      title:'定位中',
      keepAlive:false
    },
    beforeEnter: (to, from, next) => {
        let positionData = localStorage.getItem('positionData') || '';
        if(positionData){
            //定位成功
            next('/main')
        }else{
            next();
        }
    },
    component: () => import(/* webpackChunkName: "position" */ '@/views/position/index.vue'),
  },
  {
    path: '/setPos',
    name: 'setPos',
    meta:{
      title:'设置定位',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "setPos" */ '@/views/setPos/index.vue'),
  },
  {
    path: '/map',
    name: 'map',
    meta:{
      title:'选址',
      keepAlive:false
    },
    component: () => import(/* webpackChunkName: "map" */ '@/views/map/index.vue'),
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next) => {
    document.title = to.meta.title ? to.meta.title : '多点超市';

    let writeArr = ['/login','/registry'];

    let token = localStorage.getItem('token') || '';

    console.log("from",to);

    //writerr.includes(to.fullPath)

    if(writeArr.some(item => to.fullPath.includes(item)) || token){
        next();
    }else{
        next(`/login?prev=${to.fullPath}`)
    }
})

export default router
